#include "settings.h"

Settings *Settings::m_instance = new Settings();

Settings::Settings() {
  QSettings settings("wingsummer", "WingElfParser");
  elf_header_color =
      settings.value("elf_header_color", QColor(Qt::green)).value<QColor>();
  program_table_color =
      settings.value("program_table_color", QColor(Qt::yellow)).value<QColor>();
  section_header_color =
      settings.value("section_header_color", QColor(Qt::red)).value<QColor>();
  symbol_color =
      settings.value("symbol_color", QColor(Qt::cyan)).value<QColor>();
  dysymbol_color =
      settings.value("dysymbol_color", QColor(Qt::magenta)).value<QColor>();
  addmeta = settings.value("addmeta", true).value<bool>();
}

Settings *Settings::instance() { return m_instance; }

QColor Settings::getColor(SettingColor color) {
  switch (color) {
  case SettingColor::elf_header_color:
    return elf_header_color;
    break;
  case SettingColor::program_table_color:
    return program_table_color;
    break;
  case SettingColor::section_header_color:
    return section_header_color;
    break;
  case SettingColor::symbol_color:
    return symbol_color;
    break;
  case SettingColor::dysymbol_color:
    return dysymbol_color;
    break;
  }
  return QColor();
}

void Settings::getAllColor(QColor &elf_header, QColor &program_table,
                           QColor &section_header, QColor &symbol,
                           QColor &dysymbol) {
  elf_header = elf_header_color;
  program_table = program_table_color;
  section_header = section_header_color;
  symbol = symbol_color;
  dysymbol = dysymbol_color;
}

void Settings::saveColor(SettingColor color, QColor newcolor) {
  QSettings settings("wingsummer", "WingElfParser");
  switch (color) {
  case SettingColor::elf_header_color: {
    elf_header_color = newcolor;
    settings.setValue("elf_header_color", newcolor);
  } break;
  case SettingColor::program_table_color: {
    program_table_color = newcolor;
    settings.setValue("program_table_color", newcolor);
  } break;
  case SettingColor::section_header_color: {
    section_header_color = newcolor;
    settings.setValue("section_header_color", newcolor);
  } break;
  case SettingColor::symbol_color: {
    symbol_color = newcolor;
    settings.setValue("symbol_color", newcolor);
  } break;
  case SettingColor::dysymbol_color: {
    dysymbol_color = newcolor;
    settings.setValue("dysymbol_color", newcolor);
  } break;
  }
}

void Settings::saveAllColor(QColor elf_header, QColor program_table,
                            QColor section_header, QColor symbol,
                            QColor dysymbol) {
  QSettings settings("wingsummer", "WingElfParser");
  elf_header_color = elf_header;
  program_table_color = program_table;
  section_header_color = section_header;
  symbol_color = symbol;
  dysymbol_color = dysymbol;
  settings.setValue("elf_header_color", elf_header_color);
  settings.setValue("program_table_color", program_table_color);
  settings.setValue("section_header_color", section_header_color);
  settings.setValue("symbol_color", symbol_color);
  settings.setValue("dysymbol_color", dysymbol_color);
}

bool Settings::addMetadata() { return addmeta; }

void Settings::setAddMetadata(bool b) { addmeta = b; }
